package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.ClazzCountDataDTO;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentDegreeDataDTO;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> queryStudents(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        // 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 执行查询
        Page<Student> studentPage = studentMapper.queryStudents(name, degree, clazzId);

        // 封装分页结果
        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setTotal(studentPage.getTotal());
        pageResult.setRows(studentPage.getResult());

        return pageResult;
    }

    @Override
    public void addStudent(Student student) {
        // 设置创建时间和更新时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);

        // 插入数据
        studentMapper.insertStudent(student);
    }

    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.getStudentById(id);
    }

    @Override
    public void updateStudent(Student student) {
        // 设置更新时间
        student.setUpdateTime(LocalDateTime.now());

        // 更新数据
        studentMapper.updateStudent(student);
    }

    @Override
    public void deleteStudents(List<Integer> ids) {
        studentMapper.deleteStudents(ids);
    }

    @Override
    public void violationHandle(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }
    // 在 com.itheima.service.impl.StudentServiceImpl 类中添加以下方法


    @Override
    public List<StudentDegreeDataDTO> getStudentDegreeData() {
        List<Map<String, Object>> dataList = studentMapper.countStudentDegreeData();
        return dataList.stream().map(map -> {
            StudentDegreeDataDTO dto = new StudentDegreeDataDTO();
            dto.setName((String) map.get("name"));
            dto.setValue(((Long) map.get("value")).intValue());
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public ClazzCountDataDTO getStudentCountData() {
        List<Map<String, Object>> dataList = studentMapper.countStudentByClazz();

        List<String> clazzList = dataList.stream()
                .map(map -> (String) map.get("clazzName"))
                .collect(Collectors.toList());

        List<Integer> countList = dataList.stream()
                .map(map -> ((Long) map.get("studentCount")).intValue())
                .collect(Collectors.toList());

        return new ClazzCountDataDTO(clazzList, countList);
    }

}
